package com.lxf.clouduser.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lxf.clouduser.annotate.RateLimit;
import com.lxf.clouduser.bean.DataType;
import com.lxf.clouduser.utils.IpAddressUtils;
import com.lxf.clouduser.utils.RecommendUtils;
import com.lxf.clouduser.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 罗兴发
 * @data 2025/7/16 9:32
 * @description：
 */
@RestController
@CrossOrigin  //跨域
@RequestMapping("/v1")
@Slf4j
public class FeignServeController {

    @Resource
    private RecommendUtils recommendUtils;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private IpAddressUtils ipAddressUtils;


    /**
     * 根据名字获取编码类型
     * @param json
     * @return
     */
    @PostMapping("/getGoodCode")
    public int getGoodCode(@RequestBody JSONObject json){
        String name = json.getString("name");
        DataType product = DataType.getTypeByDesc(name);
        Integer typeCode = product.getNum();
        return typeCode;
    }

    /**
     * 根据编码类型获取名称以及描述
     * @param json
     * @return
     */
    @PostMapping("/getDataNameAndDesc")
    public String getDataNameAndDesc(@RequestBody JSONObject json){
        JSONObject j = new JSONObject();

        Integer type = json.getInteger("obj_type");
        //根据类型获取相应信息
        DataType dataType = DataType.getTypeByNum(type);
        String name = dataType.getName();
        String desc = dataType.getDesc();

        j.put("name",name);
        j.put("desc",desc);

        return JSONObject.toJSONString(j, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 根据用户ID，推荐数量、推荐类型
     * @param json
     * @return
     */
    @PostMapping("/getRecommend")
    public List<String> getRecommend(@RequestBody JSONObject json){
        Integer userId = json.getInteger("user_id");
        Integer recommendations = json.getInteger("num");
        Integer type = json.getInteger("obj_type");
        List<String> list = recommendUtils.recommend(userId, recommendations, type);
        return list;
    }

    /**
     * 根据key获取token是否存在
     * @param json
     * @return
     */
    @PostMapping("/getRedisData")
    public String getRedisData(@RequestBody JSONObject json){
        String keys = json.getString("redis_key");
        log.info("key为：{}",keys);
        Object o = redisUtils.get(keys);
        log.info(keys+"的数据值为：{}",o);
        if (null!=o){
            return String.valueOf(o);
        }
        return null;
    }

    /**
     * redis存储
     * @param json
     * @return
     */
    @PostMapping("/addRedisData")
    public void addRedisData(@RequestBody JSONObject json){

        //key值
        String keys = json.getString("keys");

        //存储数据
        Object data = json.get("data");

        //时间
        Integer times = json.getInteger("times");

        //作数据存储
        if (null!=times)redisUtils.set(keys,data,times);
        else redisUtils.set(keys,data);
    }

    /**
     * redis数据删除
     * @param json
     * @return
     */
    @PostMapping("/delRedisData")
    public void delRedisData(@RequestBody JSONObject json){

        //key值
        String keys = json.getString("keys");

       redisUtils.del(keys);
    }

    /**
     * 是否存在redis数据
     * @param json
     * @return
     */
    @PostMapping("/existsRedisData")
    public boolean existsRedisData(@RequestBody JSONObject json){

        //key值
        String keys = json.getString("keys");

        boolean exists = redisUtils.exists(keys);

        return exists;
    }
}
